package de.blitzcoder.cddatabase.gui.tracktable;

import de.blitzcoder.cddatabase.data.CD;
import de.blitzcoder.cddatabase.data.Track;
import de.blitzcoder.cddatabase.gui.datatable.DataTableRow;

/**
 *
 * @author blitzcoder
 */
public class TrackTableRow implements DataTableRow {

    private Track track;
    private CD cd;

    public TrackTableRow(Track track, CD cd) {
        this.track = track;
        this.cd = cd;
    }

    public CD getCD() {
        return cd;
    }

    @Override
    public Object getColumn(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return track.getName();
            case 1:
                return track.getArtist().equals(Track.NO_ARTIST) ? cd.getArtist() : track.getArtist();
            case 2:
                return cd.getName();
            case 3:
                return cd.getID();
            case 4:
                return Track.convertSecondsToString(track.getDuration());
        }
        return "Error";
    }

    @Override
    public boolean matches(String str) {
        if (str.length() == 0)
            return true;
        String[] search = new String[] {
            track.getArtist(),
            track.getName(),
            cd.getArtist(),
            cd.getName(),
            cd.getYear()+""
        };
        str = str.toLowerCase();
        for (String s:search) {
            if (s.toLowerCase().indexOf(str) != -1) {
                return true;
            }
        }
        return false;
    }

}
